﻿using BlogSystem.Services.Attributes;
using BookStore.DataLayer;
using BookStore.Models;
using BookStore.Services.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ValueProviders;

namespace BookStore.Services.Controllers
{
    public class StoresController : BaseApiController
    {
        public IQueryable<StoreModel> GetAll()
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BookstoreContext();

                var storeEntities = context.Stores;
                var models =
                    (from storeEntity in storeEntities
                     select new StoreModel()
                     {
                         Id = storeEntity.Id,
                         Name = storeEntity.StoreName,
                         City = storeEntity.City
                     });

                return models;
            });

            return responseMsg;
        }

        public StoreModelFull Get(int id)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BookstoreContext();

                var storeEntity = context.Stores.FirstOrDefault(x => x.Id == id);
                if (storeEntity == null)
                {
                    throw new InvalidOperationException("There is no such store");
                }

                var model = new StoreModelFull
                {
                    Id = storeEntity.Id,
                    Name = storeEntity.StoreName,
                    City = storeEntity.City,
                    Books = (from bookEntity in storeEntity.Books
                             select new BookModel()
                             {
                                 Id = bookEntity.Id,
                                 Title = bookEntity.Title,
                                 Author = bookEntity.Author,
                                 Store = bookEntity.Store.StoreName,
                                 Genre = bookEntity.Genre,
                                 RentCost = bookEntity.RentCost,
                                 Pages = bookEntity.Pages,
                                 IsAvailable = bookEntity.IsAvailable ? "True" : "False"
                             })
                };
                return model;
            });

            return responseMsg;
        }

        public HttpResponseMessage PostStore(StoreModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BookstoreContext();
                    using (context)
                    {
                        var user = context.Users.FirstOrDefault(
                            usr => usr.SessionKey == sessionKey);
                        if (user == null || !user.IsAdmin)
                        {
                            throw new InvalidOperationException("Invalid sessionKey");
                        }

                        var store = new Store()
                        {
                            StoreName = model.Name,
                            City = model.City
                        };

                        store = context.Stores.Add(store);
                        context.SaveChanges();

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                            store.Id);
                        return response;
                    }
                });

            return responseMsg;
        }
    }
}
